package idiet.dao.hibernate;

import idiet.bean.FoodMaterial;
import idiet.bean.FoodMaterialCategory;
import idiet.dao.SqlStatementHelper;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository("foodMaterialCategoryDao")
public class FoodMaterialCategoryDao {
	@Autowired
	SessionFactory sessionFactory;
	@Autowired
	SqlStatementHelper sqlStatementHelper;
	
	@SuppressWarnings("unchecked")
	public List<FoodMaterialCategory> getFoodMaterialCategoriesByMaterialName(String materialCategoryName){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(FoodMaterialCategory.class)
				.add(Restrictions.like("name", materialCategoryName))
				.list();
	}
	@SuppressWarnings("unchecked")
	public List<FoodMaterialCategory> getFoodMaterialCategories(){
		return this.sessionFactory
				.openSession()
				.createCriteria(FoodMaterialCategory.class)
				.list();
	}
	@SuppressWarnings("unchecked")
	public List<FoodMaterialCategory> getParentFoodMaterialCategories(){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(FoodMaterialCategory.class)
				.add(Restrictions.isNull("parentCategory"))
				.list();
	}
	@SuppressWarnings("unchecked")
	public List<FoodMaterialCategory> getChildFoodMaterialCategories(FoodMaterialCategory parent){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(FoodMaterialCategory.class)
				.add(Restrictions.eq("parentCategory", parent))
				.list();
	}
	
	public static void main(String[] args){
		FoodMaterialCategoryDao dao=new FoodMaterialCategoryDao();
		List<FoodMaterialCategory> materials= dao.getFoodMaterialCategories();
		System.out.println(materials.size());
	}
}
